<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Login Application (Auth), Login (Form process) Module (Backend)
 * Started: 30 September, 2009 7:17 PM
 * Script version: 1.0
 +------------------------------------
 */

class module implements module_interface{
	public function returninitvars(){
		global $lang;
		$this->info["page_title"] = $lang->getByKey("admin.login.authrequired");
		$this->info["page_description"] = "";
		$this->info["module_version"] = "1.0";
		return $this->info;
	}

	public function construct(){ # call our initialize functions
		if(isset($_SESSION["cf_admin_username"])){
			@header("Location: admin.php?app=home");
		}
		elseif(!isset($_POST['username'], $_POST['password'])){
			@header("Location: ?app=login");
		}
	}
	
	public function run(){ # This is basically a stripped down version of the frontend form.
		global $view, $main, $lang;
		if(($user = new user($_POST['username'], "username")) !== false){
			if($user->getStatus() == true){
				if(strcmp($main->makePassword(stripslashes($_POST['password'])), $user->getPassHash()) == 0){
					if($user->group->is_admin() == 1){
						$_SESSION["cf_username"] = $_POST['username'];
						$_SESSION["cf_admin_username"] = $_POST['username'];
						$view->writeIn($view->inlineSuccess($lang->getByKey("user.login.success"), "admin.php"));
					}
					else{
						$view->writeIn($view->inlineError($lang->getByKey("admin.login.noaccess")));
					}
				}
				else{
					$view->writeIn($view->inlineError($lang->getByKey("user.login.error.password")));
				}
			}
			else{
				$view->writeIn($view->inlineError($lang->getByKey("user.login.error.username")));
			}
		}
		else{
			$view->writeIn($view->inlineError($lang->getByKey("core.error.internal")));
		}
	}

	public function destruct(){}
}
